package com.keyroy.game.puzzle_rpg.dialogs;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.ToolItem;
import org.eclipse.wb.swt.SWTResourceManager;

import com.keyroy.game.puzzle_rpg.models.attribute.Effect;
import com.keyroy.game.puzzle_rpg.models.attribute.Param;
import com.keyroy.game.puzzle_rpg.models.interfaces.EventListener;
import com.keyroy.game.puzzle_rpg.utils.UIUtil;
import org.eclipse.swt.widgets.TableColumn;

public class EffectEditor extends Shell {
	private Table table;
	private Effect effect;

	/**
	 * Launch the application.
	 * 
	 * @param args
	 */
	public static void main(String args[]) {
		try {
			Display display = Display.getDefault();
			EffectEditor shell = new EffectEditor(display, null);
			shell.open();
			shell.layout();
			while (!shell.isDisposed()) {
				if (!display.readAndDispatch()) {
					display.sleep();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * @wbp.parser.constructor
	 */
	public EffectEditor(Display display, final EventListener<Effect> listener) {
		this(display, null, listener);
	}

	public EffectEditor(Display display, Effect eff, final EventListener<Effect> listener) {

		super(display, SWT.DIALOG_TRIM);
		setLayout(new FormLayout());
		if (eff == null) {
			this.effect = new Effect();
		} else {
			this.effect = eff;
		}

		Button btnNewButton = new Button(this, SWT.NONE);
		btnNewButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if (listener != null) {
					listener.onEvent(effect);
				}
			}
		});
		FormData fd_btnNewButton = new FormData();
		btnNewButton.setLayoutData(fd_btnNewButton);
		btnNewButton.setText("\u5B8C\u6210");

		table = new Table(this, SWT.BORDER | SWT.FULL_SELECTION);
		fd_btnNewButton.left = new FormAttachment(table, -104);
		fd_btnNewButton.top = new FormAttachment(table, 6);
		fd_btnNewButton.right = new FormAttachment(table, 0, SWT.RIGHT);
		FormData fd_table = new FormData();
		fd_table.top = new FormAttachment(0, 10);
		fd_table.bottom = new FormAttachment(100, -43);
		fd_table.left = new FormAttachment(0, 10);
		fd_table.right = new FormAttachment(0, 434);
		table.setLayoutData(fd_table);
		table.setHeaderVisible(true);
		table.setLinesVisible(true);

		ToolBar toolBar = new ToolBar(this, SWT.FLAT | SWT.RIGHT);
		FormData fd_toolBar = new FormData();
		fd_toolBar.bottom = new FormAttachment(btnNewButton, 0, SWT.BOTTOM);
		fd_toolBar.left = new FormAttachment(table, 0, SWT.LEFT);

		TableColumn tblclmnNewColumn = new TableColumn(table, SWT.NONE);
		tblclmnNewColumn.setWidth(100);
		tblclmnNewColumn.setText("\u540D\u79F0");

		TableColumn tblclmnNewColumn_1 = new TableColumn(table, SWT.NONE);
		tblclmnNewColumn_1.setWidth(100);
		tblclmnNewColumn_1.setText("\u503C");

		TableColumn tblclmnNewColumn_2 = new TableColumn(table, SWT.NONE);
		tblclmnNewColumn_2.setWidth(100);
		tblclmnNewColumn_2.setText("\u51FA\u73B0\u6982\u7387");
		toolBar.setLayoutData(fd_toolBar);

		ToolItem tltmNewItem = new ToolItem(toolBar, SWT.NONE);
		tltmNewItem.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				UIUtil.open(new ParamEditor(getDisplay(), new EventListener<Param>() {

					@Override
					public void onEvent(Param t) {
						effect.add(t);
						reset();
					}
				}));
			}
		});
		tltmNewItem.setImage(SWTResourceManager.getImage(EffectEditor.class, "/res/add_obj.gif"));
		tltmNewItem.setText("\u6DFB\u52A0");

		ToolItem tltmNewItem_1 = new ToolItem(toolBar, SWT.NONE);
		tltmNewItem_1.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				int index = table.getSelectionIndex();
				if (index != -1) {
					effect.remove(index);
					reset();
				}
			}
		});
		tltmNewItem_1.setImage(SWTResourceManager.getImage(EffectEditor.class, "/res/remove_correction.gif"));
		tltmNewItem_1.setText("\u5220\u9664");

		ToolItem tltmNewItem_2 = new ToolItem(toolBar, SWT.NONE);
		tltmNewItem_2.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				final int index = table.getSelectionIndex();
				if (index != -1) {
					UIUtil.open(new ParamEditor(getDisplay(), effect.get(index), new EventListener<Param>() {

						@Override
						public void onEvent(Param t) {
							effect.get(index).copy(t);
							effect.add(t);
							reset();
						}
					}));
				}

			}
		});
		tltmNewItem_2.setImage(SWTResourceManager.getImage(EffectEditor.class, "/res/debugt_obj.gif"));
		tltmNewItem_2.setText("\u4FEE\u6539");
		createContents();
	}

	/**
	 * Create contents of the shell.
	 */
	protected void createContents() {
		setText("\u7F16\u8F91\u6548\u679C");
		setSize(450, 300);

	}

	@Override
	protected void checkSubclass() {
	}

	public void reset() {
		table.removeAll();

		if (effect != null) {
			for (int i = 0; i < effect.params.size(); i++) {
				Param valueAttribute = effect.params.get(i);
				TableItem tableItem = new TableItem(table, SWT.NONE);
				tableItem.setText(valueAttribute.getString());
			}
		}
	}
}
